package com.duowei.kitchenshow.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.adapter.RvBottomAdapter;
import com.duowei.kitchenshow.adapter.RvBottomAdapter2;
import com.duowei.kitchenshow.adapter.RvLeftAdapter;
import com.duowei.kitchenshow.adapter.RvRightAdapter;
import com.duowei.kitchenshow.adapter.SpacesItemDecoration;
import com.duowei.kitchenshow.bean.Cfpb;
import com.duowei.kitchenshow.bean.Cfpb_csbh;
import com.duowei.kitchenshow.bean.Cfpb_item;
import com.duowei.kitchenshow.bean.Cfpb_lbbm;
import com.duowei.kitchenshow.event.ShowIn;
import com.duowei.kitchenshow.event.ShowLoad;
import com.duowei.kitchenshow.event.ShowOut;
import com.duowei.kitchenshow.httputils.Post;
import com.duowei.kitchenshow.service.MyTimeTask;
import com.duowei.kitchenshow.sound.KeySound;
import com.duowei.kitchenshow.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseMainFragment extends Fragment implements BaseQuickAdapter.OnRecyclerViewItemClickListener {
    private Timer mTimer;
    private MyTimeTask mTimeTask;
    private View mViewLoad;

    private int mLength=0;
    private float mOutTime=0;
    private KeySound mSound;

    public BaseMainFragment() {
        // Required empty public constructor
    }
    public static String sql;

    private RvLeftAdapter mLeftAdapter;
    private RvRightAdapter mRightAdapter;
    private RvBottomAdapter mBottomAdapter;
    private RvBottomAdapter2 mRvBottomAdapter2;

    private List<Cfpb_item>mCfpbItemList;

    private List<Cfpb_item>mCfpbItemListAll=new ArrayList<>();

    public void setSql(String sql) {
        this.sql=sql;
        if(mTimeTask!=null){
            mTimeTask.setSql(sql);
        }
    }

    public void setLeftAdapter(RvLeftAdapter leftAdapter) {
        mLeftAdapter = leftAdapter;
    }

    public void setRightAdapter(RvRightAdapter rightAdapter) {
        mRightAdapter = rightAdapter;
    }

    public void setBottomAdapter(RvBottomAdapter bottomAdapter){
        mBottomAdapter=bottomAdapter;
    }

    public void setRvBottomAdapter2(RvBottomAdapter2 rvBottomAdapter2) { mRvBottomAdapter2 = rvBottomAdapter2; }

    public void showLoad(){
        mViewLoad.setVisibility(View.VISIBLE);
    }
    public void hideLoad(){
        mViewLoad.setVisibility(View.GONE);
    }

    public void setCfpb(Cfpb[]cfpbs){
        float outTime = 0;
        for(int i=0;i<cfpbs.length;i++){
            //超时单品
            String cssj = cfpbs[i].getCssj();
            if (!TextUtils.isEmpty(cssj) && cfpbs[i].getFzs() > Integer.parseInt(cssj)) {
                outTime = outTime + cfpbs[i].getSl();
            }
        }
        if(cfpbs.length>mLength&&outTime>mOutTime){//新订单、超时单品
            mSound.playSound('0',0);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSound.playSound('1',0);
                }
            },2000);
        }else if(cfpbs.length>mLength){//新订单
            mSound.playSound('0',0);
        }else if(outTime>mOutTime){//超时单品
            mSound.playSound('1',0);
        }
        mLength=cfpbs.length;
        mOutTime=outTime;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_main, container, false);
        mSound = KeySound.getContext(getActivity());

        RecyclerView rvLeft = inflate.findViewById(R.id.rv_left);
        RecyclerView rvRight = inflate.findViewById(R.id.rv_right);
        RecyclerView rvBottom = inflate.findViewById(R.id.rv_bottom);
        rvLeft.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rvLeft.addItemDecoration(new SpacesItemDecoration(2));
        rvLeft.setAdapter(mLeftAdapter);

        rvRight.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rvRight.addItemDecoration(new SpacesItemDecoration(2));
        rvRight.setAdapter(mRightAdapter);

        rvBottom.addItemDecoration(new SpacesItemDecoration(3));
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvBottom.setLayoutManager(manager);
        if(mBottomAdapter!=null){
            rvBottom.setAdapter(mBottomAdapter);
        }else if(mRvBottomAdapter2!=null){
            rvBottom.setAdapter(mRvBottomAdapter2);
        }

        mViewLoad = inflate.findViewById(R.id.ll_load);

        mRightAdapter.setOnRecyclerViewItemClickListener(this);
        return inflate;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        mTimer = new Timer();
        mTimeTask = new MyTimeTask(sql);
        mTimer.schedule(mTimeTask,0,5000);
    }

    @Override
    public void onStop() {
        super.onStop();
        mTimer.cancel();
        EventBus.getDefault().unregister(this);
    }

    public void notifyLeftAdapter(List<Cfpb>cfpbList){
        mLeftAdapter.setNewData(cfpbList);

        mCfpbItemListAll.clear();
        for(int i=0;i<cfpbList.size();i++){
            List<Cfpb_item> itemList = cfpbList.get(i).getItemList();
            for(int j=0;j<itemList.size();j++){
                Cfpb_item cfpbItem = itemList.get(j);
                if("1".equals(cfpbItem.getBy10())){
                    mCfpbItemListAll.add(cfpbItem);
                }
            }
        }
        if(mCfpbItemListAll.size()>0){
            EventBus.getDefault().post(new ShowOut(mCfpbItemListAll));
        }else{
            EventBus.getDefault().post(new ShowIn());
        }
    }

    public void notifyRightAdapter(List<Cfpb_item>cfpbItemList){
        mCfpbItemList=cfpbItemList;
        mRightAdapter.setNewData(cfpbItemList);
    }

    public void notifyBottomAdapter(List<Cfpb_lbbm>cfpbLbbms){
        mBottomAdapter.setNewData(cfpbLbbms);
    }

    public void notifyBottomeAdapter2(List<Cfpb_csbh>cfpbCsbhs){
        mRvBottomAdapter2.setNewData(cfpbCsbhs);
    }

    @Subscribe
    public void setLoad(ShowLoad event){
        if(event.isShow()){
            mViewLoad.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onItemClick(View view, int i) {
        mViewLoad.setVisibility(View.VISIBLE);
        Cfpb_item cfpbItem = mCfpbItemList.get(i);
        if("1".equals(cfpbItem.getBy10())){
            Post.getInstance().postUpdate("0",cfpbItem.xh,sql);
        }else{
            Post.getInstance().postUpdate("1",cfpbItem.xh,sql);
        }
    }
}
